package com.iscale.util;

import org.apache.commons.lang3.RandomStringUtils;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * Created by andychen83 on 2017/3/10.
 */
public class ToolsUtils {
    @Test
    public void indexof() throws Exception {
        String readMessage = "CF041EAA0218089C16018702026A06DC";
        byte[] array = new byte[20];
        array[0] = (byte) 0xCF;
        array[1] = (byte) 0x04;
        array[2] = (byte) 0x1E;
        array[3] = (byte) 0xAA;
        array[4] = (byte) 0x02;
        array[5] = (byte) 0x18;
        array[6] = (byte) 0x08;
        array[7] = (byte)0x9C;
        array[8] = (byte) 0x16;
        array[9] = (byte) 0x01;
        array[10] = (byte) 0x87;
        array[11] = (byte) 0x02;
        array[12] = (byte) 0x02;
        array[13] = (byte) 0x6A;
        array[14] = (byte) 0x06;
        array[15] = (byte) 0xDC;

        int weight = hexToTen(readMessage.substring(8, 12));
        System.out.println(weight);

        int weight2 = (array[4] << 8) | (array[5] & 0xff);     //bytes 4,5 contains weight data

        System.out.println(weight2);

        System.out.println(convertKgToLb(weight2,0.1f));
    }

    private static float convertKgToLb(int weight, float scale) {
        float weightinLBS = (float) (weight * scale * 2.2046);
        String formate = String.format("%f", weightinLBS);
        String subSctring = formate.substring(0, formate.indexOf(".") + 2);
        return Float.valueOf(subSctring);
    }

    /**16进制转10进制*/
    public static int hexToTen(String hex) {
        if (null == hex || (null != hex && "".equals(hex))) {
            return 0;
        }
        return Integer.valueOf(hex, 16);
    }

    public String identifyFileTypeUsingFilesProbeContentType(final String fileName) throws IOException {
        String fileType = "Undetermined";
        final File file = new File(fileName);
        try
        {
            fileType = Files.probeContentType(file.toPath());
        }
        catch (IOException ioException)
        {
            System.out.println(
                    "ERROR: Unable to determine file type for " + fileName
                            + " due to exception " + ioException);
        }
        return fileType;
    }

}
